﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class SynthesizeCompare : System.Web.UI.Page
    {
        protected int sid = 0;
        IList<StudentScoreExtendInfo> MidAllScoreInfos = new List<StudentScoreExtendInfo>();
        IList<StudentScoreExtendInfo> HighAllScoreInfos = new List<StudentScoreExtendInfo>();
        protected decimal lei1 = 0, lei2 = 0, lei3 = 0;
        protected decimal wen1 = 0, wen2 = 0, wen3 = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!PageValidate.QueryStringIsEmpty("Sid"))
                {
                    try
                    {
                        sid = int.Parse(PageValidate.QueryString("Sid"));
                        GraduationInfo graduationInfo = Graduation.GetSingleInfo(sid);
                        wen1 = graduationInfo.Literacy_One.Value;
                        wen2 = graduationInfo.Literacy_Tow.Value;
                        wen3 = graduationInfo.Literacy_Three.Value;
                        lei1 = graduationInfo.Science_One.Value;
                        lei2 = graduationInfo.Science_Tow.Value;
                        lei3 = graduationInfo.Science_Three.Value;
                        string[] MidIds = graduationInfo.MidExamRoomId.Split(',');
                        string[] HighIds = graduationInfo.HighExamRoomId.Split(',');
                        MidAllScoreInfos = StudentScoreExtend.GetList().Where(t => MidIds.Contains(t.ExamRoomId.ToString())).ToList();
                        HighAllScoreInfos = StudentScoreExtend.GetList().Where(t => HighIds.Contains(t.ExamRoomId.ToString())).ToList();


                        BindTable();
                    }
                    catch
                    {

                    }
                }
                else
                {

                }
            }
        }

        private void BindTable()
        {
            StringBuilder html = new StringBuilder();
            var areaIds = HighAllScoreInfos.GroupBy(t => t.AreaId).Select(p => new { AreaId = p.Key }).ToList();
            var HighNum = HighAllScoreInfos.GroupBy(t => t.StudentId).Select(p => new { StudentId = p.Key, CountScore = p.Sum(t => t.CountScore) }).ToList();
            foreach (var id in areaIds)
            {
                AreaInfo areaInfo = Area.GetSingleInfo(id.AreaId);
                html.Append("<h3>" + areaInfo.Name + "</h3>");
                html.Append("<table class='module_table' style='width:100%'>");
                html.Append("<thead id='TableHeader'>");
                html.Append("<tr>");
                html.Append("<th>学校名称</th>");
                html.Append("<th>中考人数</th>");
                html.Append("<th>高考人数</th>");

                html.Append("<th>中考一线</th>");
                html.Append("<th>全区比率</th>");
                html.Append("<th>一本人数</th>");
                html.Append("<th>全区比率</th>");
                html.Append("<th>一线增量</th>");
                html.Append("<th>增量比</th>");

                html.Append("<th>中考二线</th>");
                html.Append("<th>全区比率</th>");
                html.Append("<th>二本人数</th>");
                html.Append("<th>全区比率</th>");
                html.Append("<th>二线增量</th>");
                html.Append("<th>增量比</th>");

                html.Append("<th>中考三线</th>");
                html.Append("<th>全区比率</th>");
                html.Append("<th>三本人数</th>");
                html.Append("<th>全区比率</th>");
                html.Append("<th>三线增量</th>");
                html.Append("<th>增量比</th>");

                html.Append("</tr>");
                html.Append("</thead>");
                html.Append("<tbody>");
                html.Append(BindSchool(id.AreaId));
                html.Append("</tbody>");
                html.Append("</table>");
            }
            HtmlModle.InnerHtml = html.ToString();
        }

        private string BindSchool(int areaid)
        {
            StringBuilder html = new StringBuilder();
            IList<SchoolInfo> schoolInfos = School.GetSchoolInfoByAreaId(areaid);
            IList<StudentScoreExtendInfo> HighAreaScore = HighAllScoreInfos.Where(t => t.AreaId == areaid).ToList();
            IList<StudentScoreExtendInfo> MidAreaScore = MidAllScoreInfos.Where(t => t.AreaId == areaid).ToList();

            var wen = HighAllScoreInfos.Where(t => t.SubjectType == (int)SubjectTypes.文科).GroupBy(t => t.StudentId).Select(p => new { StudentId = p.Key, CountScore = p.Sum(t => t.CountScore) }).ToList();
            var lei = HighAllScoreInfos.Where(t => t.SubjectType == (int)SubjectTypes.理科).GroupBy(t => t.StudentId).Select(p => new { StudentId = p.Key, CountScore = p.Sum(t => t.CountScore) }).ToList();

            var wen_one = wen.Where(t => t.CountScore >= wen1).ToList();
            var lei_one = lei.Where(t => t.CountScore >= lei1).ToList();

            var wen_tow = wen.Where(t => t.CountScore < wen1 && t.CountScore >= wen2).ToList();
            var lei_tow = lei.Where(t => t.CountScore < lei1 && t.CountScore >= lei2).ToList();

            var wen_thr = wen.Where(t => t.CountScore < wen2 && t.CountScore >= wen3).ToList();
            var lei_thr = lei.Where(t => t.CountScore < lei2 && t.CountScore >= lei3).ToList();

            foreach (SchoolInfo schoolInfo in schoolInfos)
            {
                if (HighAreaScore.Where(t => t.SchoolId == schoolInfo.Id).Count() > 0)
                {
                    var HighSchoolScore = HighAreaScore.Where(t => t.SchoolId == schoolInfo.Id).GroupBy(t => t.StudentId).Select(p => new { StudentId = p.Key, CountScore = p.Sum(t => t.CountScore) }).ToList();
                    var MidSchoolScore = MidAreaScore.Where(t => t.SchoolId == schoolInfo.Id).GroupBy(t => t.StudentId).Select(p => new { StudentId = p.Key, CountScore = p.Sum(t => t.CountScore) }).OrderByDescending(p => p.CountScore).ToList();
                    IList<int> studentIds = HighSchoolScore.Select(t => t.StudentId).ToList();
                    int school_one = wen_one.Where(t => studentIds.Contains(t.StudentId)).Count() + lei_one.Where(t => studentIds.Contains(t.StudentId)).Count();
                    decimal percent_one = (wen_one.Count + lei_one.Count) == 0 ? 0 : Math.Round((decimal)school_one / (wen_one.Count + lei_one.Count), 2);

                    int school_tow = wen_tow.Where(t => studentIds.Contains(t.StudentId)).Count() + lei_tow.Where(t => studentIds.Contains(t.StudentId)).Count();
                    decimal percent_tow = (wen_tow.Count + lei_tow.Count) == 0 ? 0 : Math.Round((decimal)school_tow / (wen_tow.Count + lei_tow.Count), 2);

                    int school_thr = wen_thr.Where(t => studentIds.Contains(t.StudentId)).Count() + lei_thr.Where(t => studentIds.Contains(t.StudentId)).Count();
                    decimal percent_thr = (wen_thr.Count + lei_thr.Count) == 0 ? 0 : Math.Round((decimal)school_thr / (wen_thr.Count + lei_thr.Count), 2);

                    var MidNum = MidAllScoreInfos.GroupBy(t => t.StudentId).Select(p => new { StudentId = p.Key, CountScore = p.Sum(t => t.CountScore) }).OrderByDescending(p => p.CountScore).ToList();

                    int index_one = (int)(MidNum.Count * percent_one);
                    decimal MidScore_OneLine = MidNum[index_one].CountScore;
                    int MidSchoolNum_one = MidSchoolScore.Where(t => t.CountScore >= MidScore_OneLine).Count();
                    decimal MidScore_OnePercent = index_one == 0 ? 0 : Math.Round((decimal)MidSchoolNum_one / index_one * 100, 2);
                    decimal add_one = (MidSchoolNum_one == 0 || school_one == 0) ? 0 : (school_one > MidSchoolNum_one ? Math.Round((decimal)school_one / MidSchoolNum_one * 100, 2) : -Math.Round((decimal)MidSchoolNum_one / school_one * 100, 2));

                    int index_tow = (int)(MidNum.Count * percent_tow);
                    decimal MidScore_TowLine = MidNum[index_tow].CountScore;
                    int MidSchoolNum_tow = MidSchoolScore.Where(t => t.CountScore >= MidScore_TowLine).Count();
                    decimal MidScore_TowPercent = index_tow == 0 ? 0 : Math.Round((decimal)MidSchoolNum_tow / index_tow * 100, 2);
                    decimal add_tow = (school_tow == 0 || MidSchoolNum_tow == 0) ? 0 : (school_tow > MidSchoolNum_tow ? Math.Round((decimal)school_tow / MidSchoolNum_tow * 100, 2) : -Math.Round((decimal)MidSchoolNum_tow / school_tow * 100, 2));

                    int index_thr = (int)(MidNum.Count * percent_thr);
                    decimal MidScore_ThrLine = MidNum[index_thr].CountScore;
                    int MidSchoolNum_thr = MidSchoolScore.Where(t => t.CountScore >= MidScore_ThrLine).Count();
                    decimal MidScore_ThrPercent = index_thr == 0 ? 0 : Math.Round((decimal)MidSchoolNum_thr / index_thr * 100, 2);
                    decimal add_thr = (school_thr == 0 || MidSchoolNum_thr == 0) ? 0 : (school_thr > MidSchoolNum_thr ? Math.Round((decimal)school_thr / MidSchoolNum_thr * 100, 2) : -Math.Round((decimal)MidSchoolNum_thr / school_thr * 100, 2));

                    html.Append("<tr>");
                    html.Append("<td>" + schoolInfo.Name + "</td>");
                    html.Append("<td>" + MidSchoolScore.Count + "</td>");
                    html.Append("<td>" + HighSchoolScore.Count + "</td>");

                    html.Append("<td>" + MidSchoolNum_one + "</td>");
                    html.Append("<td>" + MidScore_OnePercent + "%</td>");
                    html.Append("<td>" + school_one + "</td>");
                    html.Append("<td>" + Math.Round(percent_one * 100, 2) + "%</td>");
                    html.Append("<td>" + (school_one - MidSchoolNum_one) + "</td>");
                    html.Append("<td>" + add_one + "%</td>");

                    html.Append("<td>" + MidSchoolNum_tow + "</td>");
                    html.Append("<td>" + MidScore_TowPercent + "%</td>");
                    html.Append("<td>" + school_tow + "</td>");
                    html.Append("<td>" + Math.Round(percent_tow * 100, 2) + "%</td>");
                    html.Append("<td>" + (school_tow - MidSchoolNum_tow) + "</td>");
                    html.Append("<td>" + add_tow + "%</td>");

                    html.Append("<td>" + MidSchoolNum_thr + "</td>");
                    html.Append("<td>" + MidScore_ThrPercent + "%</td>");
                    html.Append("<td>" + school_thr + "</td>");
                    html.Append("<td>" + Math.Round(percent_thr * 100, 2) + "%</td>");
                    html.Append("<td>" + (school_thr - MidSchoolNum_thr) + "</td>");
                    html.Append("<td>" + add_thr + "%</td>");
                    html.Append("</tr>");
                }
            }

            return html.ToString();
        }
    }
}
